﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            var sql = "select * from Students";

            var dt = DbHelper.GetDataTable(sql);


            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;





        }





        //查询
        private void label2_Click(object sender, EventArgs e)
        {
            var Name = textBox1.Text;
            var sql = string.Format("select * from Students where Name like '%{0}%'",Name);
            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        //增加
        private void label3_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            var res = form2.ShowDialog();


            if (res == DialogResult.OK)
            {
                var sql = "select * from Students";
                var dt = DbHelper.GetDataTable(sql);

                dataGridView1.DataSource = dt;
            }
        }

        //删除
        private void label4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var row = dataGridView1.SelectedRows[0];

                var Id = (int)row.Cells[0].Value;

                var res = MessageBox.Show("确定删除吗？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Students where Id={0}",Id);
                    var rowCount = DbHelper.AddOrEdiSave(sql);
                    if (rowCount > 0)
                    {
                        var sqlStu = "select * from Students";
                        var dt = DbHelper.GetDataTable(sqlStu);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("删除成功","提示");
                    }


                }


            }
            else
            {
                MessageBox.Show("没有选择任何的行","提示");

            }


        }


        //更新
        private void label5_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
                var Name = (string)dataGridView1.SelectedRows[0].Cells[1].Value;
                var Age = (int)dataGridView1.SelectedRows[0].Cells[2].Value;
                var Score = (int)dataGridView1.SelectedRows[0].Cells[3].Value;
                Form3 form = new Form3(Id,Name,Age,Score);
               var res= form.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var names = textBox1.Text;
                    var sql = string.Format("select * from Students where Name like '%{0}%'",names);
                    var dt = DbHelper.GetDataTable(sql);

                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("你取消了操作");
                }
                
            }
            else
            {
                MessageBox.Show("您未选中任何行","提示");
            }
           
        }
    }
}
